#ifndef FOC
#define FOC
#include "at32f413_wk_config.h"




#define UDC         36.0f     // 直流母线电压

#define DEAD_TIME   0        // 死区时间计数值




#define Imax  16.0f
#define Udc 36.0f
#define Umax  Udc/sqrt3
//#define Umax  Udc
#define PI 3.14159265358979f
#define PWM_PERIOD 6000
#define ENCODER_PPR (1000)
#define POLE_PAIR_NUM 3 // 电机极对数

#define PWM_FREQ 16000       // PWM频率


#define ADC_REF_V                   (float)(3.3f)
#define SAMPLE_RES                  (float)(0.003f)                       //(double)(0.01f)
#define AMP_GAIN                    (float)(2.7f)                       //(double)(15.0f)
#define SAMPLE_CURR_CON_FACTOR      (float)(ADC_REF_V/4096.0f/AMP_GAIN/SAMPLE_RES)  0.099464f
#define SAMPLE_CURR_CON_FACTOR_1     0.099464f
#define pi                         3.14159265358979f

#define   ANGULAR_ALIGNMENT_TIME      PWM_FREQ*0.5f                    //500ms

#define REDUCTION_RATIO        1
#define  ROTO_RATIO             (4*1000-1)

#define sqrt3           1.732051f
// 用于交换变量的宏

#define ENCODE_COUNT     1000*4



#define SWAP(a, b) do { float temp = a; a = b; b = temp; } while(0)
typedef struct
{
  float Vd;
  float Vq;
}VOLTAGE_DQ_DEF;


typedef struct
{
  float Ialpha;
  float Ibeta;
}CURRENT_ALPHA_BETA_DEF;

typedef struct
{
  float Cos;
  float Sin;
}TRANSF_COS_SIN_DEF;


typedef struct
{
  float Ia;
  float Ib;
  float Ic;
}CURRENT_ABC_DEF;

typedef struct
{
  uint16_t Ia;
  uint16_t Ib;
  uint16_t Ic;
}Current_Offset;

typedef enum 
{
IDLE, INIT, START, RUN, STOP, BRAKE, WAIT, FAULT,STOP_TO_RUN
} SystStatus_t;




typedef struct
{
  float Id;
  float Iq;
} CURRENT_DQ_DEF;



// PID参数结构体
typedef struct {
  float Kp;    // 比例增益
  float Ki;    // 积分增益
  float Kd;    // 微分增益
  float prev_error;  // 前一次误差
  float integral;    // 积分项
  float output;      // PID输出
  float max_output;  // 最大输出值
  float min_output;  // 最小输出值
  float max_integral;  // 最大积分值
  float min_integral;  // 最小积分值
} PIDController;

typedef struct 
{
  int encode_old;                  /* 上一次计数值 */
  int encode_now;                  /* 当前计数值 */
  float speed;                     /* 编码器速度 */
} ENCODE_TypeDef;











extern volatile uint8_t is_upcount;
extern volatile uint8_t task_1ms; //1ms标志位
extern volatile uint8_t task_10ms; //10ms标志位
extern uint8_t State;

extern int32_t g_timx_encode_count;

extern float speed_motor; 

extern int16_t speed_set;

//初始化，开启定时器及互补通道，配置PWM输出
void board_config(void);


void SVPWM_Calculate_GPT_2(float V_alpha, float V_beta);


void InverseParkTransform(float V_d, float V_q, float theta, float *V_alpha, float *V_beta);
void foc_current_loop(void);


void Start_Up(void);
void current_read_foc_3shunt(void);

//获取电流偏置
void Get_Current_Offset(uint16_t num_samples);
float get_encode_angle(void)  ;
void ClarkeTransform(CURRENT_ABC_DEF Iabc, CURRENT_ALPHA_BETA_DEF* IalphaBeta);
void ParkTransform(CURRENT_ALPHA_BETA_DEF IalphaBeta, float theta, CURRENT_DQ_DEF* Idq);
void PID_Init(PIDController *pid, float Kp, float Ki, float Kd, float max_output, float min_output, float max_integral, float min_integral);
float PID_Compute(PIDController *pid, float setpoint, float measured_value);
void speed_computer(int32_t encode_now, uint8_t ms);
int32_t gtim_get_encode(void);
void foc_speed_loop(void);
void Stop(void);
void process_received_data(uint8_t* data, uint8_t length);
void state_switch(void);
void stop_to_run(void);
void Modulate_Vec_Tim(float* T1, float* T2);
#endif